/*这是测试定时器的代码*/
#include <sys/timerfd.h>
#include <iostream>
#include <unistd.h>
int main(int argc, char **argv)
{
    int timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (timer_fd < 0)
    {
        std::cerr << "定时器文件描述符创建失败！" << std::endl;
        return -1;
    }
    itimerspec new_timer;
    new_timer.it_interval.tv_sec = 3;
    new_timer.it_interval.tv_nsec = 0;
    new_timer.it_value.tv_sec = 1;
    new_timer.it_value.tv_nsec = 0;
    timerfd_settime(timer_fd, 0, &new_timer, nullptr);
    u_int64_t times;
    while (1)
    {
        int ret = read(timer_fd, &times, sizeof(times));
        if (ret < 0)
        {
            std::cerr << "从定时器读取数据失败！" << std::endl;
            break;
        }
        printf("距离上次定时超时了%d次！\n", times);
    }
    return 0;
}